package com.example.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MyCommentActivity extends AppCompatActivity {
    private RecyclerView rv_commentNews;
    private List<Data> dataList = new ArrayList<>();
    private NewsListAdapter adapter;
    private EditText edtComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_comment);
        setTitle("我的评论");

        edtComment = findViewById(R.id.edt_comment);

        rv_commentNews = findViewById(R.id.rv_commentNews);
        //rv一定要设置的
        rv_commentNews.setLayoutManager(new LinearLayoutManager(MyCommentActivity.this));
        adapter = new NewsListAdapter(dataList);
        rv_commentNews.setAdapter(adapter);

        //获取评论的新闻列表
        getCommentNewsData();
    }

    private void getCommentNewsData() {
        String userid = BmobUser.getCurrentUser(BmobUser.class).getObjectId();

        BmobQuery<Comment> commentBmobQuery = new BmobQuery<>();
        commentBmobQuery.addWhereEqualTo("userid",userid);
        commentBmobQuery.addWhereEqualTo("is_content",false);

        commentBmobQuery.findObjects(new FindListener<Comment>() {
            @Override
            public void done(List<Comment> list, BmobException e) {
                if (e==null){
                    dataList.clear();
                    if (list!=null&&list.size()> 0){
                        for (Comment comment : list){
                            String newsjson = comment.getNewsjson();
                            Data data = new Gson().fromJson(newsjson,Data.class);
//                            edtComment.setText(comment.getContent());
//                            notifyAll();
                            dataList.add(data);
                        }
                        adapter.changeData(dataList);
                    }
                }
            }
        });
    }
}
